package nl.windgazer.arachne.mixins;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.AfterRenderTemplate;
import org.apache.tapestry5.annotations.BeforeRenderTemplate;
import org.apache.tapestry5.annotations.SupportsInformalParameters;
import org.apache.tapestry5.dom.Element;
import org.apache.tapestry5.ioc.annotations.Inject;

@SupportsInformalParameters
public class RenderInformalElement {

	//Injects and Persists

	@Inject
	private ComponentResources _cr;

	//Properties and Parameters

	private static final String DEFAULT_ELEMENT = "div";

	//Components

	//Events

	/**
	 * Renders the informal element, which is what is specified in the calling page/component. This
	 * way this component can be wrapped in any type of html-element.
	 */
	@BeforeRenderTemplate
	void renderInformalsEtc(MarkupWriter writer) {
		Element informal = writer.element(_cr.getElementName(DEFAULT_ELEMENT)); //Create an element with the found element-name, or if that was a component, use default (div).

	    _cr.renderInformalParameters(writer); //Render informal parmaters defined on the element in the template...
	    informal.attribute("id", _cr.getId()); //Render the unique id of this component, unless specified as informal parameter
	}

	/**
	 * Cleanup of the informal element rendering.
	 * @param writer
	 */
	@AfterRenderTemplate
	void finishElementEtc(MarkupWriter writer) {
		writer.end();//Close the generated element
	}

	//Other (check for business-logic, it should not remain here)

	public String getId() {
		return _cr.getId();
	}

}
